package models

import (
	"log"
	"time"
	"xorm.io/builder"
)

type StuUnregisterRecord struct {
	Id           int64     `json:"id" xorm:"pk autoincr"`
	PerformerUid int64     `json:"performer_uid"`             // 执行解除备案操作的用户ID
	SchoolId     int       `json:"school_id"`                 // 驾校 ID
	UserId       int       `json:"user_id"`                   // 学员用户ID
	StudentId    int64     `json:"student_id"`                // 学员 ID
	BeforeNumber int64     `json:"before_number"`             // 删除学员之前驾校学员注册数
	ReasonId     string    `json:"reason_id"`                 // 解除原因 1. 转校, 2. 驾校换平台, 3. 退学, 4. 学员自动注销, 9 其他
	Status       string    `json:"status" xorm:"default '1'"` // 状态
	Ip           string    `json:"ip" `                       // 地址
	ChangeStatus string    `xorm:"comment('是否退还名额') index ENUM('0','1')"`
	CreateTime   time.Time `json:"create_time" xorm:"created"`
	Resp         string    `json:"resp" xorm:"resp"`
	UpdateTime   time.Time `json:"update_time" xorm:"updated"`
	Creator      int       `json:"creator" xorm:"creator"`
}

type UnStudent struct {
	Total         int
	ReturnCount   int
	UnReturnCount int
}

func GetById(id string) (list StuUnregisterRecord, err error) {
	_, err = engine.Table("dts_stu_unregister_record").
		Where("id = ? ", id).
		Get(&list)
	return
}

func (s *StuUnregisterRecord) GetBySchoolId(filter builder.Cond, school_id int) (list UnStudent, err error) {
	_, err = engine.Table("dts_stu_unregister_record").Alias("sd").
		Select("count(*)as total,count( IF ( sd.change_status = '1', 1, NULL ) ) AS return_count ,count( IF ( sd.change_status = '0', 1, NULL ) ) AS un_return_count ").
		Join("left", []string{"dts_user", "u"}, "u.id = sd.user_id").
		Join("left", []string{"dts_student", "st"}, "u.id = st.user_id").
		Where("sd.school_id = ? ", school_id).And(filter).
		Get(&list)
	return
}

type UnStudentList struct {
	Id              int    `json:"id"`
	UserId          int    `json:"user_id"`
	Name            string `json:"name"`
	Remark          string `json:"remark"`
	ReidNumber      string `json:"reid_number"`
	TrainingCarType string `json:"training_car_type"`
	CreateTime      string `json:"create_time"`
	OperationStatus string `json:"operation_status"`
	SchoolName      string `json:"school_name"`
	Ip              string `json:"ip"`
	ChangeStatus    string `json:"change_status"`
}

func GetUnList(filter builder.Cond, size int, offset int) (count int64, list []*UnStudentList, err error) {
	err = engine.Table("dts_stu_unregister_record").Alias("sud").
		Select("sud.id,u.name,sud.user_id,u.remark,sud.change_status,u.sex,u.reid_number,sud.create_time,st.training_car_type,st.operation_status,sl.name_short as school_name,sud.ip").
		Join("left", []string{"dts_student", "st"}, "st.id = sud.student_id").
		Join("left", []string{"dts_user", "u"}, "u.id = st.user_id").
		Join("left", []string{"dts_school", "sl"}, "sl.id = st.school_id").
		And(filter).
		Limit(size, offset).Desc("sud.id").Find(&list)
	count, _ = engine.Table("dts_stu_unregister_record").Alias("sud").
		Join("left", []string{"dts_student", "st"}, "st.id = sud.student_id").
		Join("left", []string{"dts_user", "u"}, "u.id = st.user_id").
		Join("left", []string{"dts_school", "sl"}, "sl.id = st.school_id").
		And(filter).
		Count()
	return
}

func (r *StuUnregisterRecord) Add() (ok bool, err error) {
	affectedRows, err := engine.Table("dts_stu_unregister_record").Insert(r)
	if err != nil {
		return false, err
	}

	return affectedRows == 1, nil
}

type StuUnregisterRecordInfo struct {
	Id              int64     `json:"id"` // 解除备案ID
	Name            string    `json:"name"`
	Phone           string    `json:"phone"`
	SchoolId        int       `json:"school_id"`
	IdNumber        string    `json:"id_number"`
	Reason          string    `json:"reason"`            // 解除备案原因
	TrainingCarType string    `json:"training_car_type"` // 培训车型
	RegisterTime    string    `json:"register_time"`     // 学员报名时间
	UnregisterTime  time.Time `json:"unregister_time"`   // 学员解除备案时间
}

func (r *StuUnregisterRecord) GetList(id string) (list StuUnregisterRecordInfo, err error) {
	//list = make([]*StuUnregisterRecordInfo, 0)
	_, err = engine.Table("dts_stu_unregister_record").Alias("r").
		Select("r.id,r.school_id, u.name, u.remark as phone,u.reid_number as id_number, stu.training_car_type, stu.sign_in_time as register_time, r.create_time as unregister_time, r.reason_id as reason").
		Join("INNER", []string{"dts_user", "u"}, "r.user_id = u.id").
		Join("INNER", []string{"dts_student", "stu"}, "stu.user_id = r.user_id").
		Where("r.id = ?", id).
		Get(&list)
	if err != nil {
		log.Println(err)
		return list, err
	}
	return list, nil
}
